import sys
import os

pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd + '../')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'riscv_build_dataset.settings')

import django

django.setup()

import requests
import json
from bs4 import BeautifulSoup
from config import BASE_URL
from obs.models import obs_project, obs_package
from tqdm import tqdm


def get_packages_from_url(project_name: str, repository_name: str, architecture_name: str):
    # Seems that accessing this page does not need to log in
    # Page URL: https://build.tarsier-infra.com/project/show/openEuler:23.09:BaseOS:01
    # API URL: https://build.tarsier-infra.com/build/openEuler:23.09:BaseOS:01/20230921/riscv64/
    # Get Packages in a Project

    packages = []

    cookies = {
        '_obs_api_session': os.environ['OBS_COOKIE'],
    }

    headers = {
        'Accept': 'application/xml',
        'charset': 'utf-8',
        # 'Authorization': 'Basic ' + b64credential,
    }

    response = requests.get(
        f'{BASE_URL}/build/{project_name}/{repository_name}/{architecture_name}',
        cookies=cookies,
        headers=headers)

    print(response.text)

    soup = BeautifulSoup(response.text, 'lxml')

    packages = []
    for package in soup.find_all('entry'):
        packages.append(package['name'])

    return packages


def save_to_db(packages):
    project = obs_project.objects.get(project_name=project_name)
    # tmp = []
    for package in tqdm(packages):
        # p = obs_package(
        #     project=project,
        #     package_name=package
        # )
        # tmp.append(p)
        obs_package.objects.get_or_create(project=project, package_name=package)
    # obs_package.objects.bulk_create(tmp, batch_size=100, ignore_conflicts=True)


if __name__ == '__main__':
    project_name = os.environ.get('OBS_PROJECT')
    repository_name = os.environ.get('OBS_REPO')
    arch_name = os.environ.get('OBS_ARCH')
    # packages = get_packages_from_url(project_name, repository_name, architecture_name)
    # parse_json()

    # project_name = 'openEuler:22.09'
    # repository_name = 'openEuler_2209_self'
    # architecture_name = 'riscv64'
    packages = get_packages_from_url(project_name, repository_name, arch_name)

    save_to_db(packages)
